package com.kongge.formlike.service;

import android.content.Context;
import android.view.View;

import com.google.gson.Gson;
import com.kongge.formlike.bean.FormBean;
import com.kongge.formlike.bean.ViewBean;
import com.kongge.formlike.service.check.CheckView;
import com.kongge.formlike.service.check.ICheckItem;
import com.kongge.formlike.service.rule.IRule;
import com.kongge.formlike.service.rule.RuleFactory;
import com.kongge.formlike.util.ProtocolContants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * author:kongge
 * date:2018/7/20
 * layout:
 * description:
 */

public class ViewParser {

    private Class defaultCheckViewClass;

    public ViewParser(Class defaultCheckViewClass) {
        this.defaultCheckViewClass = defaultCheckViewClass;
    }

    public FormBean parseView(View rootView, String jsonStr) {
        Gson gson = new Gson();
        Map<String, Object> configMap = (Map<String, Object>) gson.fromJson(jsonStr, Object.class);
        if (rootView == null || configMap == null || defaultCheckViewClass == null) {
            return null;
        }
        FormBean formBean = new FormBean();
        Context context = rootView.getContext();
        try {
            formBean.setConfigMap(configMap);

            List<Map<String, Object>> itemList = (List<Map<String, Object>>) configMap.get(ProtocolContants.STR_ITEM);
            List<ViewBean> viewBeanList = new ArrayList<>();
            for (Map<String, Object> itemMap : itemList) {
                ViewBean viewBean = new ViewBean();

                String valueId = (String) itemMap.get(ProtocolContants.STR_ID);
                int viewId = context.getResources().getIdentifier(valueId, "id", context.getPackageName());
                View view = rootView.findViewById(viewId);
                ICheckItem checkItem = (ICheckItem) defaultCheckViewClass.newInstance();
                checkItem.setView(view);
                viewBean.setValueId(valueId);
                viewBean.setCheckItem(checkItem);

                String paramName = (String) itemMap.get(ProtocolContants.STR_PARAMNAME);
                viewBean.setParamName(paramName);

                String type = (String) itemMap.get(ProtocolContants.STR_TYPE);
                viewBean.setType(type);

                Map<String, Object> rulesMap = (Map) itemMap.get(ProtocolContants.STR_RULES);
                if (rulesMap != null) {
                    List<IRule> ruleList = new ArrayList<>();
                    for (String ruleStr : rulesMap.keySet()) {
                        IRule rule = RuleFactory.getRule(ruleStr);
                        if (rule != null) {
                            rule.setRuleName(ruleStr);
                            Object value = rulesMap.get(ruleStr);
                            List<Object> paramList;
                            if (value instanceof List) {
                                paramList = (List<Object>) value;
                            } else {
                                paramList = new ArrayList<>();
                                paramList.add(value);
                            }
                            rule.setParam(paramList);
                        }
                        ruleList.add(rule);
                    }
                    viewBean.setRuleList(ruleList);
                }
                viewBeanList.add(viewBean);
            }
            formBean.setViewBeanList(viewBeanList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return formBean;
    }


}
